﻿namespace Furesoft.Styling.Design.Style
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using FastColoredTextBoxNS;

    using Furesoft.Styling.Design.Style.Contexts;
    using Furesoft.Styling.Winforms.Controls;

    public partial class StyleEditor : Form
    {
        private AutocompleteMenu menu;

        public StyleEditor()
        {
            this.InitializeComponent();

            // comment
            StyleManager.Add(@"/\*.*\*/", Brushes.DarkSeaGreen, FontStyle.Italic);
            // chars
            StyleManager.Add(@"[.:#]", Brushes.Black, FontStyle.Regular);
            // selector
            StyleManager.Add(@"[#.]([\w-_.\d]*)", Brushes.Red, FontStyle.Regular);
            // event
            StyleManager.Add(@":[\w-_\d]*", Brushes.Orange, FontStyle.Regular);
            // event
            StyleManager.Add(@"[\d\w-_]*:", Brushes.Purple, FontStyle.Regular);
            // string
            StyleManager.Add(@"(""|').*?\1", Brushes.Maroon, FontStyle.Regular);

            menu = new AutocompleteMenu(this.edit);
            var items = new List<AutocompleteItem>();

            foreach (var propertyInfo in new CssPanel().GetType().GetProperties())
            {
                items.Add(new AutocompleteItem(propertyInfo.Name + ": ", null, propertyInfo.Name));
            }

            StyleManager.Add(new UnderlineColorStyle().GetColorPattern(), new UnderlineColorStyle());
            edit.ToolTipNeeded += edit_ToolTipNeeded;
            
            IntellisenseManager.AddContext(typeof(Color), new ColorContext());

            /*foreach (var item in IntellisenseManager.GetItems(typeof(Color)))
            {
                items.Add(item);
            }*/

            menu.MinFragmentLength = 2;
            menu.Items.SetAutocompleteItems(items);
        }

        void edit_ToolTipNeeded(object sender, ToolTipNeededEventArgs e)
        {
            if (Regex.IsMatch(e.HoveredWord, new UnderlineColorStyle().GetColorPattern()))
            {
                var ch = new ColorHint(Color.FromName(e.HoveredWord), edit);
                edit.AddHint(edit.GetRange(e.Place.iChar, e.Place.iChar + e.HoveredWord.Length), ch, false, true, false);
            }
            else
            {
                if (!string.IsNullOrEmpty(e.HoveredWord))
                {
                    e.ToolTipTitle = e.HoveredWord;
                    e.ToolTipText = "This is tooltip for '" + e.HoveredWord + "'";
                }
            }
        }

        private void edit_TextChanged(object sender, TextChangedEventArgs e)
        {
            foreach (var textStyle in StyleManager.Styles)
            {
                e.ChangedRange.ClearStyle(textStyle);
            }
            foreach (var textStyle in StyleManager.patterns)
            {
                e.ChangedRange.SetStyle(textStyle.Value, textStyle.Key);
            }

            e.ChangedRange.ClearFoldingMarkers();
            e.ChangedRange.SetFoldingMarkers("{", "}");
             
            e.ChangedRange.SetFoldingMarkers(@"/\*", @"\*/"); //allow to collapse comment block

            if (e.ChangedRange.tb[e.ChangedRange.ToLine].Text.Contains("Color:"))
            {
                this.menu = new AutocompleteMenu(this.edit);
                this.menu.Items.SetAutocompleteItems(IntellisenseManager.GetItems(typeof(Color)));
            }
            else
            {
                this.menu = new AutocompleteMenu(this.edit);
            }
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.edit.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.edit.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.edit.Paste();
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.edit.ShowFindDialog();
        }

        private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.edit.ShowReplaceDialog();
        } 
    }
}
